package com.hongyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hongyi.common.framework.constant.CacheConstants;
import com.hongyi.common.framework.constant.Constants;
import com.hongyi.common.framework.exception.ServiceException;
import com.hongyi.common.framework.utils.bean.MapstructUtils;
import com.hongyi.common.framework.utils.common.CollectionUtils;
import com.hongyi.common.framework.utils.string.StringUtils;
import com.hongyi.common.mybatis.page.PageData;
import com.hongyi.common.mybatis.page.PageQuery;
import com.hongyi.common.redis.utils.RedisUtils;
import com.hongyi.system.domain.SysDictData;
import com.hongyi.system.domain.dto.SysDictDataDto;
import com.hongyi.system.domain.vo.SysDictDataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import com.hongyi.system.mapper.SysDictDataMapper;
import com.hongyi.system.service.ISysDictDataService;

import java.util.Collections;
import java.util.List;

@Service
public class SysDictDataServiceImpl  implements ISysDictDataService {

    @Autowired
    private SysDictDataMapper sysDictDataMapper;

    private LambdaQueryWrapper<SysDictData> buildQueryWrapper(SysDictDataDto sysDictDataDto) {
        LambdaQueryWrapper<SysDictData> lqw = Wrappers.lambdaQuery();
        lqw.eq(sysDictDataDto.getDictSort() != null, SysDictData::getDictSort, sysDictDataDto.getDictSort());
        lqw.like(StringUtils.isNotBlank(sysDictDataDto.getDictLabel()), SysDictData::getDictLabel, sysDictDataDto.getDictLabel());
        lqw.eq(StringUtils.isNotBlank(sysDictDataDto.getDictType()), SysDictData::getDictType, sysDictDataDto.getDictType());
        lqw.orderByAsc(SysDictData::getDictSort);
        return lqw;
    }

    /**
     * 字典类型分页列表
     *
     * @param sysDictDataDto 字典对象
     * @param pageQuery 分页对象
     * @return
     */
    @Override
    public PageData<SysDictDataVo> selectPageDictDataList(SysDictDataDto sysDictDataDto, PageQuery pageQuery) {
        LambdaQueryWrapper<SysDictData> lqw = buildQueryWrapper(sysDictDataDto);
        IPage<SysDictDataVo> sysDictDataVoIPage = sysDictDataMapper.selectVoPage(pageQuery.build(), lqw);
        return PageData.success(sysDictDataVoIPage);
    }

    /**
     * 根据字典数据ID查询信息
     *
     * @param dictCode 字典数据ID
     * @return 字典数据
     */
    @Override
    public SysDictDataVo selectDictDataById(Long dictCode) {
        return sysDictDataMapper.selectVoById(dictCode);
    }

    /**
     * 根据字典类型查询字典数据
     *
     * @param dictType 字典类型
     * @return 字典数据集合信息
     */
    @Cacheable(cacheNames = CacheConstants.SYS_DICT, key = "#dictType")
    @Override
    public List<SysDictDataVo> selectDictDataByType(String dictType) {
        return getSysDictDataVoList(dictType);
    }

    /**
     * 新增保存字典数据信息
     *
     * @param sysDictDataDto@return 结果
     */
    @CachePut(cacheNames = CacheConstants.SYS_DICT, key = "#sysDictDataDto.dictType")
    @Override
    public List<SysDictDataVo> insertDictData(SysDictDataDto sysDictDataDto) {
        SysDictData sysDictData = MapstructUtils.convert(sysDictDataDto, SysDictData.class);
        int row = sysDictDataMapper.insert(sysDictData);
        if (row>0){
            return getSysDictDataVoList(sysDictDataDto.getDictType());
        }
        throw new ServiceException("操作失败");
    }

    /**
     * 修改保存字典数据信息
     *
     * @param sysDictDataDto 字典数据信息
     * @return 结果
     */
    @CachePut(cacheNames = CacheConstants.SYS_DICT, key = "#sysDictDataDto.dictType")
    @Override
    public List<SysDictDataVo> updateDictData(SysDictDataDto sysDictDataDto) {
        SysDictData sysDictData = MapstructUtils.convert(sysDictDataDto, SysDictData.class);
        int row = sysDictDataMapper.updateById(sysDictData);
        if (row>0){
          return getSysDictDataVoList(sysDictDataDto.getDictType());
        }
        throw new ServiceException("操作失败");
    }

    /**
     * 批量删除字典数据信息
     *
     * @param dictCodes 需要删除的字典数据ID
     */
    @Override
    public void deleteDictDataByIds(Long[] dictCodes) {
        for (Long dictCode : dictCodes) {
            SysDictData data = sysDictDataMapper.selectById(dictCode);
            sysDictDataMapper.deleteById(dictCode);
            RedisUtils.deleteObject(CacheConstants.GLOBAL_REDIS_KEY +CacheConstants.SYS_DICT +":"+ data.getDictType());
        }
    }

    private List<SysDictDataVo> getSysDictDataVoList(String dictType){
        List<SysDictDataVo> sysDictDataVos = sysDictDataMapper.selectVoList(new LambdaQueryWrapper<SysDictData>()
                .eq(SysDictData::getDictType, dictType)
                .eq(SysDictData::getStatus, Constants.OK)
                .orderByAsc(SysDictData::getDictSort));
        return CollectionUtils.isNotEmpty(sysDictDataVos) ? sysDictDataVos : null;
    }

}
